package com.example.activitytest;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    private ActivityResultLauncher mActivityResultLauncher;
    private EditText mName;
    private EditText mPassword;
    private Button mLogin;
    private Button mRegister;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initActivityResultLauncher();
        mName = findViewById(R.id.name);
        mPassword = findViewById(R.id.password);
        mLogin = findViewById(R.id.login);
        mRegister = findViewById(R.id.register);
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivityResultLauncher.launch(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }
    private void initActivityResultLauncher() {
        mActivityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        int resultCode = result.getResultCode();
                        if (resultCode == RESULT_OK) {
                            Intent intent = result.getData();
                            if (intent != null) {
                                mName.setText(intent.getStringExtra("name"));
                                mPassword.setText(intent.getStringExtra("password"));
                            }
                        }
                    }
                });
    }
}
